import com.Module.prms.operation;

import flash.net.URLRequest;
import flash.net.navigateToURL;

import j2as3.collection.HashMap;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.LinkBar;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import spark.components.Button;

[Bindable]
private var menuId:String;
private var indexPage:index= new index; 
private var indexvar:index = new index;
private var condition:Object;
public var  opts:ArrayCollection = new ArrayCollection;
public static var optsmap:HashMap = new HashMap();
//end

private function getFaultMessage(faultStr:String):void{
	var index:int = faultStr.indexOf(":");
	var fStr:String = null;
	if(index !=-1){
		fStr = faultStr.substring(index+2,faultStr.length);
	}else {
		fStr = faultStr;
	}
	if(fStr =="已超时，请再登录"){
		Alert.yesLabel = '确定';
		Alert.show(fStr,"错误信息",1,this,handler);
	}else {
		if(fStr!="null")
		{
		Alert.show(fStr,"错误信息");
		}
	}
}
private function handler(event:CloseEvent):void{ 
	if(event.detail==Alert.YES){ 
		navigateToURL(new URLRequest("index.html"),"_top");
	} 
}
public function faultHandler(event:FaultEvent):void{
	getFaultMessage(event.fault.faultString);
	
}

public function faultHandlerCallback(event:FaultEvent,fn:Function):void{
	var faultStr:String = event.fault.faultString;
	if(fn != null){
		fn();
	}
	getFaultMessage(event.fault.faultString);
}

//add panzs
public function setMenuId(item : String):void{
	menuId = item;
}
public function setIndex(idx : index):void{
	indexPage = idx;
}

//回调函数,控制按钮是否显示
public function getOperationByRoleIdAndMenuIdHandler(event:ResultEvent):void{
	opts=event.result as ArrayCollection;
	//testlist = event.result as ArrayCollection;
	try{
//	for(var i=0;i<index.orgsOptList.length;i++){
//			var buttont:Object = this[index.orgsOptList.getItemAt(i).toString()];
//			var button:Button=null;
//			if(buttont!=null)
//			{
//				if(buttont instanceof Button)
//				{	
//						button =buttont as Button;
//						button.visible=true;
//						button.includeInLayout=true;
//				}
//			}
//		//this[index.orgsOptList.getItemAt(i).toString()].visible=true;
//		//this[index.orgsOptList.getItemAt(i).toString()].includeInLayout=true;
//		}
	for(var j=0;j<opts.length;j++)
	{
		if(optsmap.getItem(opts.getItemAt(j).toString())==null)
		{
			optsmap.put(opts.getItemAt(j).toString(),opts.getItemAt(j).toString());
		}
	}
	for(var i=0;i<opts.length;i++){
		var buttont:Object =this[opts.getItemAt(i).toString()];;
		var button:Button=null;
		if(buttont!=null)
		{
			
			if(buttont instanceof Button)
			{	
				button =buttont as Button;
				button.visible=true;
				button.includeInLayout=true;
			}
		}
				//this[opts.getItemAt(i).toString()].visible=true;
				//this[opts.getItemAt(i).toString()].includeInLayout=true;
	}
	
	}catch(error:Error){
		
		//Alert.show(error.message);
		trace("组件显示失败！");
	}
}
//超链接
public function getOperationByRoleIdAndMenuIdHandler4Link(evnet:FlexEvent):void{
	var test:mx.controls.LinkBar = evnet.currentTarget as mx.controls.LinkBar;
	//Alert.show("test.id="+test.id+"   "+optsmap.getItem(test.id));
	if(optsmap.getItem(test.id)!=null)
	{
		test.visible=true;
		test.includeInLayout=true;
	}else
	{
		for(var i:Number=0;i<opts.length;i++){
			
			if(test.id==opts.getItemAt(i).toString()){
				test.visible=true;
				test.includeInLayout=true;
			}
		}
	}
	
//	for(var i:Number=0;i<index.orgsOptList.length;i++){
//		
//		if(test.id==index.orgsOptList.getItemAt(i).toString()){
//			test.visible=true;
//			test.includeInLayout=true;
//		}
//	}
	
//	for(var i:Number=0;i<opts.length;i++){
//		
//		if(test.id==opts.getItemAt(i).toString()){
//			test.visible=true;
//			test.includeInLayout=true;
//		}
//	}
}

//end